<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="创作类型" prop="createTitle">
      <el-input v-model="dataForm.createTitle" placeholder="创作类型"></el-input>
    </el-form-item>
    <el-form-item label="创作内容" prop="createContent">
      <el-input v-model="dataForm.createContent" placeholder="创作内容"></el-input>
    </el-form-item>
    <el-form-item label="创作图片" prop="createImage">
      <el-input v-model="dataForm.createImage" placeholder="创作图片"></el-input>
    </el-form-item>
    <el-form-item label="创建日期" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建日期"></el-input>
    </el-form-item>
    <el-form-item label="创作字数" prop="createWords">
      <el-input v-model="dataForm.createWords" placeholder="创作字数"></el-input>
    </el-form-item>
    <el-form-item label="创作类型" prop="createType">
      <el-input v-model="dataForm.createType" placeholder="创作类型"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          createTitle: '',
          createContent: '',
          createImage: '',
          createTime: '',
          createWords: '',
          createType: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          createTitle: [
            { required: true, message: '创作类型不能为空', trigger: 'blur' }
          ],
          createContent: [
            { required: true, message: '创作内容不能为空', trigger: 'blur' }
          ],
          createImage: [
            { required: true, message: '创作图片不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          createWords: [
            { required: true, message: '创作字数不能为空', trigger: 'blur' }
          ],
          createType: [
            { required: true, message: '创作类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/user/userrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.userRecord.userId
                this.dataForm.createTitle = data.userRecord.createTitle
                this.dataForm.createContent = data.userRecord.createContent
                this.dataForm.createImage = data.userRecord.createImage
                this.dataForm.createTime = data.userRecord.createTime
                this.dataForm.createWords = data.userRecord.createWords
                this.dataForm.createType = data.userRecord.createType
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/userrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'createTitle': this.dataForm.createTitle,
                'createContent': this.dataForm.createContent,
                'createImage': this.dataForm.createImage,
                'createTime': this.dataForm.createTime,
                'createWords': this.dataForm.createWords,
                'createType': this.dataForm.createType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
